/*
 *
 * @ProjectName:    my_umi_app
 * @Package:
 * @ClassName:      ad_api
 * @Description:    广告相关api
 * @Author:         SuPeiSen
 * @CreateDate:     2021/2/25 3:16 下午
 * @UpdateDate:     2021/2/25 3:16 下午
 * @UpdateRemark:   更新说明：
 * @Version:        1.0
 */

import { Requests } from '@/utils/requests';
import { ApiPath } from '@/config/api_config';
import { boolToNumOneOrZero } from '@/utils/app_fun';
import { AdItemType, AdListType } from '@/type/ad_type';

/**
 * 添加广告
 * @param param
 * @constructor
 */
export const AddAdApi = async (param: any) => {
  param.px = Number(param.px ?? 99);
  const model = await Requests.defaults.http('post', ApiPath.addAd, {
    postData: param,
  });
  return model.success;
};

/**
 * 更新广告
 * @param id
 * @param param
 * @constructor
 */
export const UpdateAdApi = async (id: number, param: any) => {
  param.id = id;
  const model = await Requests.defaults.http('put', ApiPath.updateAd, {
    postData: param,
  });
  return model.success;
};

/**
 * 获取广告
 * @constructor
 */
export const GetAdListApi = async (param: { page: number; limit: number }) => {
  const model = await Requests.defaults.http<AdListType>(
    'post',
    ApiPath.getAdList,
    {
      postData: {
        page: param.page,
        limit: param.limit,
      },
    },
  );
  return model;
};

/**
 * 根据id更新广告状态
 * @param id
 * @param param
 * @constructor
 */
export const UpdateAdStatusByIdApi = async (param: {
  id: number;
  status: boolean;
}) => {
  const model = await Requests.defaults.http('put', ApiPath.updateAdStatus, {
    postData: {
      id: param.id,
      status: boolToNumOneOrZero(param.status),
    },
  });
  return model.success;
};

/**
 * 根据id删除广告
 * @param id
 * @constructor
 */
export const DeleteAdApi = async (id: number) => {
  const model = await Requests.defaults.http('delete', ApiPath.deleteAd, {
    getData: {
      id: id,
    },
  });
  return model.success;
};

/**
 * 通过id获取广告
 * @param id
 * @constructor
 */
export const GetAdByIdApi = async (id: number) => {
  return await Requests.defaults.http<AdItemType>('get', ApiPath.getAdById, {
    getData: {
      id: id,
    },
  });
};
